package com.xinqi.common.mybatis.injector.utils;

import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.springframework.util.ClassUtils;

import java.util.Optional;

/**
 * @author linkai
 * @date 2019/08/04
 */
public final class LogicDeleteUtils {

    private static final String DELETE_TIME = "delete_time";

    private LogicDeleteUtils() {
    }

    public static String sqlLogicSet(TableInfo table) {
        String set = "SET " + table.getLogicDeleteSql(false, false);
        Optional<TableFieldInfo> op = table.getFieldList().stream().filter(
                v -> v.getColumn().equals(DELETE_TIME) && ClassUtils.isAssignable(v.getPropertyType(), Long.class))
            .findFirst();
        return op.map(v -> String.format("%s , %s=NOW() ", set, DELETE_TIME)).orElse(set);
    }

    public static String sqlDeleteTime(String sql) {
        return sql + ", " + DELETE_TIME + " = NOW() ";
    }

}
